package com.fruit.account.service.usercount;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.account.commission.CommissionDetailMapper;
import com.fruit.mapper.account.ticket.TicketDetailMapper;
import com.fruit.mapper.account.voucher.ActivityVoucherMapper;
import com.fruit.pojo.account.ticket.TicketDetail;
import com.fruit.pojo.account.usercount.UserCommissionCount;
import com.fruit.pojo.account.voucher.ActivityVoucher;
import com.fruit.sdk.account.usercount.UserCountService;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户邀请统计相关Service
 * 
 * @author Administrator
 * @version 2017.11.30 11:40
 */
@Slf4j
@Service("userCountService")
public class UserCountServiceImpl implements UserCountService {
	@Autowired
	private CommissionDetailMapper commissionDetailMapper;
	@Autowired
	private TicketDetailMapper ticketDetailMapper;
	@Autowired
	private ActivityVoucherMapper activityVoucherMapper;
	
	@Override
	public Map<String, Object> countUserCommissions(Map<String, Object> params) {
		log.info("【用户下级贡献佣金统计】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (params == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
			return result;
		}
		log.info("【用户下级贡献佣金统计】---【params】---{}", params.toString());
		if (StringUtils.isEmpty(String.valueOf(params.get("userId")))) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数用户id不能为空!"));
			return result;
		}
		try {
			// 统计用户下级贡献佣金明细
			List<UserCommissionCount> list = commissionDetailMapper.countUserCommissions(params);
			// 统计用户下级贡献佣金总金额
			Integer userId = Integer.valueOf(String.valueOf(params.get("userId")));
			String money = commissionDetailMapper.getTotalMoneyByUserId(userId);

			result.put("dataList", list);
			result.put("money", money);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【用户下级贡献佣金统计】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【用户下级贡献佣金统计】---【结束】---");
		return result;
	}

	@Override
	public Map<String, Object> getUserTicketList(String tradeId, Map<String, Object> params) {
		log.info("【用户粮票明细列表】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (params == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "getUserTicketInfos_param_error", "传入参数不能为null!"));
				return result;
			}
			if (params.get("userId") == null || StringUtils.isEmpty(String.valueOf(params.get("userId")))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数用户id不能为空!"));
				return result;
			}
			if (null == params.get("startNum") || "".equals(params.get("startNum"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参起始条数不能为空!"));
				return result;
			}
			if (null == params.get("pageNum") || "".equals(params.get("pageNum"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数显示条数不能为空!"));
				return result;
			}
			log.info("【用户粮票明细列表】---【params】---{}", params.toString());
			List<TicketDetail> list = ticketDetailMapper.getUserTicketList(params);
			result.put("dataList", list);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", "操作成功"));
		} catch (Exception e) {
			log.error("【用户粮票明细列表】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "getUserTicketInfos_exception", "系统异常"));
		}
		log.info("【用户粮票明细列表】---【结束】---{}", result.toString());
		return result;
	}
	
	@Override
	public Map<String, Object> getUserTicketDetail(String tradeId, Map<String, Object> params) {
		log.info("【用户粮票明细】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (params == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "getUserTicketInfos_param_error", "传入参数不能为null!"));
				return result;
			}
			if (params.get("id") == null || StringUtils.isEmpty(String.valueOf(params.get("id")))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数id不能为空!"));
				return result;
			}
			log.info("【用户粮票明细】---【params】---{}", params.toString());
			TicketDetail td = ticketDetailMapper.getUserTicketDetail(params);
			result.put("dataList", td);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", "操作成功"));
		} catch (Exception e) {
			log.error("【用户粮票明细】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "getUserTicketInfos_exception", "系统异常"));
		}
		log.info("【用户粮票明细】---【结束】---{}", result.toString());
		return result;
	}
	
	@Override
	public Map<String, Object> getUserVoucherList(String tradeId, Map<String, Object> params) {
		log.info("【兑换券列表】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (params == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
				return result;
			}
			if (params.get("userId") == null || StringUtils.isEmpty(String.valueOf(params.get("userId")))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数用户id不能为空!"));
				return result;
			}
			if (null == params.get("startNum") || "".equals(params.get("startNum"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参起始条数不能为空!"));
				return result;
			}
			if (null == params.get("pageNum") || "".equals(params.get("pageNum"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数显示条数不能为空!"));
				return result;
			}
			if (null == params.get("state") || "".equals(params.get("state"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数使用状态不能为空!"));
				return result;
			}
			
			log.info("【兑换券列表】---【params】---{}", params.toString());
			params.put("parentId", "1");
			List<ActivityVoucher> list = activityVoucherMapper.getUserVoucherList(params);
			log.info("【兑换券列表】---【查询结果】---list={}", list.toString());
			result.put("dataList", list);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", "操作成功"));
		} catch (Exception e) {
			log.error("【兑换券列表】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "getUserVoucherList_exception", "系统异常"));
		}
		log.info("【兑换券列表】---【结束】---{}", result.toString());
		return result;
	}
}
